package com.atguigu.gmall.fastdfs;

import org.junit.Test;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @Auther 赵乾坤
 * @Date 2020-12-27 10:57
 */
public class CompletableFutureDemo1 {
    //创建能返回或不能回返值的线程
    @Test
    public void test1() throws Exception{
        //1:创建线程
//        CompletableFuture<String> javaCompletableFuture =
//                CompletableFuture.completedFuture("java");
//        System.out.println(javaCompletableFuture.get());

        //2:创建打印线程名称的线程
//        CompletableFuture<Void> voidCompletableFuture =
//                CompletableFuture.runAsync(() -> {
//                    System.out.println(Thread.currentThread().getName());
//        });
//        System.out.println(voidCompletableFuture.get());
//        CompletableFuture<String> uCompletableFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println(Thread.currentThread().getName());
//            return "赵乾坤最棒了";
//        });
//        System.out.println(uCompletableFuture.get());

    }
    //串行   任务1 -串行-》 任务2   任务2需要任务1的返回值
    @Test
    public void test2() throws Exception{
        System.out.println(Thread.currentThread().getName());
        //1:创建线程
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1开始");
            int i = 1 + 3;
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int j = 1 / 0;
            return i;
        });
        //2:线程
        CompletableFuture<Void> voidCompletableFuture = completableFuture.thenAcceptAsync((t) -> {
            System.out.println("t是i吗：" + t);
        });
        //thenAccept() //表示：任务2直接使用任务1的线程
        //thenAccept Async () 表示： 任务2 去线程池获取一个新的线程来执行
        CompletableFuture<Integer> exceptionally =
                completableFuture.exceptionally((e) -> {
                    System.out.println(e);
                    return 1;
                });
        completableFuture.whenCompleteAsync((t,e) -> {
            System.out.println(t);
            System.out.println(e);
        });
        //等一会儿
        CompletableFuture.allOf(voidCompletableFuture,completableFuture,exceptionally).join();


}
    //并行
    @Test
    public void test3() throws Exception{
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "AAAAAA");
        return "haha";
        });
        //2:
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "BBBBB");
        });
        System.out.println(Thread.currentThread().getName()+"test3");
//        stringCompletableFuture.join();
//        voidCompletableFuture.join();
        CompletableFuture.allOf(stringCompletableFuture, voidCompletableFuture);
    }
}
